package com.wdd.aop;

import java.util.Enumeration;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect
@Component
public class AopTest {
	private static final Logger logger = LoggerFactory.getLogger(AopTest.class); 
	public AopTest(){
	        
    }
    
    @Pointcut("execution(* com.wdd.web.defaultt.controller..*.*(..))")
    public void allMethod(){}
    
    @Before("allMethod()")
    public void beforeSleep(){
    	logger.info("############################################################请求开始##############################################################");
    	ServletRequestAttributes attr = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
    	HttpServletRequest request = attr.getRequest();
    	logger.info("URL:"+request.getRequestURL().toString());
    	Enumeration<String> enu = request.getParameterNames();
    	while(enu.hasMoreElements()){
    		String name = enu.nextElement();
    		logger.info("name:{},value:{}",name,request.getParameter(name));
    	}
    	
        System.out.println("AopTest before!");
    }
    
    @AfterReturning("allMethod()")
    public void afterSleep(){
        System.out.println("AopTest after！");
        logger.info("############################################################请求结束##############################################################");
    }
}
